
'''
    Description:    a calendar-widget for Tkinter
    Version:        0.2
    Copyright:      2004 by Markus Weihs (ehm_dot_weihs_at_utanet_dot_at)
    Created:        2004-07-17
    Last modified:  2004-07-18
    Licence:        free
    Requirements:   Python,Tkinter
'''

import Tkinter as tk
from calendar import setfirstweekday, monthcalendar

class TkCalendar(tk.Frame):
    __callback = None
    def __init__(self,y,m,callback=None):
        tk.Frame.__init__(self,bd=2,relief=tk.GROOVE)
        self.year = y
        self.month = m
        if callback is not None:
            self.__callback = callback
        self.date = tk.StringVar()

        self.dayArray = ["Su","Mo","Tu","We","Th","Fr","Sa"]
        self.col = 0
        setfirstweekday(6)

        f = tk.Frame(self)
        f.pack(fill=tk.X)
        tk.Button(f,
            text='<',
            command=self.__decrease,
            relief=tk.FLAT).pack(side=tk.LEFT)
        self.l = tk.Label(f,text="%.2i.%i" % (self.month,self.year))
        self.l.pack(side=tk.LEFT,padx=12)
        tk.Button(f,
            text='>',
            command=self.__increase,
            relief=tk.FLAT).pack(side=tk.RIGHT)

        self.c = tk.Canvas(self,
            width=140,height=135,
            bg='white',bd=2,relief=tk.GROOVE)
        self.c.bind('<1>', self.__click)
        self.c.pack()
       
        self.__fill_canvas()

    def __fill_canvas(self):
        m = monthcalendar(self.year,self.month)
   
        x=13; y=13
        for col in range(len(m[0])):
            for row in range(len(m)):
                if m[row][col]==0:
                    pass
                else:
                    if col==self.col:
                        self.c.create_text(
                            col*20+x,row*20+30,
                            text="%2i" % m[row][col],fill='red',tags='day')
                    else:
                        self.c.create_text(
                            col*20+x,row*20+30,
                            text="%2i" % m[row][col],tags='day')
        for i in self.dayArray:
            self.c.create_text(x,y,text=i,fill='blue', tags='day')
            x+=20

    def __decrease(self):
        self.c.delete('day')
        if self.month == 1:
            self.year -= 1
            self.month = 12
        else:
            self.month -= 1
        self.l.configure(text="%.2i.%i" % (self.month, self.year))
        self.__fill_canvas()
   

    def __increase(self):
        self.c.delete('day')
        if self.month == 12:
            self.year += 1
            self.month = 1
        else:
            self.month += 1
        self.l.configure(text="%.2i.%i" % (self.month, self.year))
        self.__fill_canvas()

    def __click(self,event):
        x = self.c.find_closest(event.x,event.y)[0]
        try:
            day = int(self.c.itemcget(x,'text'))
            if self.__callback is not None:
                self.__callback(datetime.datetime(self.year,self.month,day))
        except: pass
